package com.cx.controller.czw;

import com.cx.interceptor.Status;
import com.cx.pojo.User;
import com.cx.redis.RedisOpsUtil;
import com.cx.service.czw.QiniuService;
import com.cx.service.czw.RegisterService;
import com.cx.service.czw.UserInfoService;
import com.cx.util.czw.MsgUtil;
import com.cx.util.czw.RandomUtil;
import com.cx.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import java.io.IOException;

@Controller
@RequestMapping("userInfo")
@MultipartConfig
public class UserInfoController {

    @Autowired
    UserInfoService userInfoService;
    @Autowired
    private QiniuService qiniuService;
    @Autowired
    RedisOpsUtil redisOpsUtil;
    @Autowired
    RegisterService registerService;


    //根据登录/注册的电话查询用户信息
    @RequestMapping("selectUserInfo")
    @ResponseBody
    public ResponseResult selectUserInfo(){
      String userId = String.valueOf(Status.map.get("id"));
//        String userId = String.valueOf(redisOpsUtil.get("id"));
        ResponseResult responseResult = new ResponseResult();
        User user = userInfoService.selectUserInfo(userId);
        if(null == user){
            responseResult.setMessage("显示信息失败");
            responseResult.setCode(2);
        }else {
            responseResult.setMessage("显示信息成功");
            responseResult.setCode(1);
            responseResult.setData(user);
        }
        return responseResult;
    }

    //根据用户的信息，电话。来修改用户信息
    @RequestMapping("updateUserInfo")
    @ResponseBody
    public ResponseResult updateUserInfo(MultipartFile file ,User user){
        System.out.println("user=" +user);
        String userId = String.valueOf(Status.map.get("id"));
//        String userId = String.valueOf(redisOpsUtil.get("id"));
        ResponseResult responseResult = new ResponseResult();
        System.out.println(userId);
        System.out.println("file=" +file);
        if(null != file) {
            try {
                String fileUrl = qiniuService.saveImage(file);
                user.setHeadPortrait(fileUrl);
                System.out.println("fileUrl =" + fileUrl);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        int affectRows = userInfoService.updateUserInfo(userId , user);
        System.out.println(affectRows);
        if(0 != affectRows){
            responseResult.setMessage("信息修改成功");
            responseResult.setCode(1);
        }else {
            responseResult.setMessage("信息修改失败");
            responseResult.setCode(2);
        }
        return responseResult;
    }

    //根据用户登录状态 右上角信息
    @RequestMapping("loginStatus")
    @ResponseBody
    public ResponseResult loginStatus(){
        ResponseResult responseResult = new ResponseResult();
//        System.out.println("loginStatus=" +Status.map.get("phone"));
        Object login = Status.map.get("login");
//        String login = String.valueOf(redisOpsUtil.get("login"));
        System.out.println("loginStatus--" +login);
        if(null != Status.map.get("login")){
            responseResult.setMessage("登录状态");
            responseResult.setCode(1);
            responseResult.setData(Status.map);
//            responseResult.setData(redisOpsUtil.get("phone"));
        }else {
            responseResult.setMessage("非登录状态");
            responseResult.setCode(2);
        }
        return responseResult;
    }

    //登录后，用户中心，如果用户名字不为空，显示姓名，为空则显示电话
    @RequestMapping("loginName")
    @ResponseBody
    public ResponseResult loginName(){
        ResponseResult responseResult = new ResponseResult();
        String userId = String.valueOf(Status.map.get("id"));
//        String userId = String.valueOf(redisOpsUtil.get("id"));
        User user = userInfoService.selectUserInfo(userId);
        String userName = user.getUserName();
        String phone = user.getUserPhone();
        if(null != userName){
            responseResult.setMessage("有昵称！");
            responseResult.setCode(1);
            responseResult.setData(userName);
        }else {
            responseResult.setMessage("未设置昵称！");
            responseResult.setCode(2);
            responseResult.setData(phone);
        }
        return responseResult;
    }
    //获取当前登录用户的手机号
     @RequestMapping("phone")
     @ResponseBody
     public ResponseResult phone(){
        Object phone = Status.map.get("phone");
//        String phone = String.valueOf(redisOpsUtil.get("phone"));
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(1);
        responseResult.setData(phone);
        return responseResult;
     }

    //更改手机
    @RequestMapping("changePhone")
    @ResponseBody
    public ResponseResult changePhone(String randomPhoneCode,String phone){
        ResponseResult responseResult = new ResponseResult();
        User user = registerService.userCheckPhone(phone);
        if(null == user) {
            if(Status.map.get("randomPhoneCode").equals(randomPhoneCode)) {
                String userId = String.valueOf(Status.map.get("id"));
                User user1 = new User();
                user1.setUserPhone(phone);
                int affectRows = userInfoService.updateUserInfo(userId , user1);
                if(0 != affectRows){
                    responseResult.setMessage("电话修改成功");
                    responseResult.setCode(1);
//                    redisOpsUtil.del("randomCode");
                    Status.map.remove("randomCode");
                }else {
                    responseResult.setMessage("电话修改失败");
                    responseResult.setCode(2);
                }
            }else {
                responseResult.setCode(4);
                responseResult.setMessage("短信验证码错误");
            }
        }else {
            responseResult.setMessage("已经注册过了");
            responseResult.setCode(3);
        }
        return responseResult;
    }

    //更改手机时，手机获取短信验证码
    @RequestMapping("getRandomPhoneCode")
    @ResponseBody
    public ResponseResult getRandomPhoneCode(String phone){
        ResponseResult responseResult = new ResponseResult();
        String randomPhoneCode = RandomUtil.createRandNum();
        Status.map.put("randomPhoneCode",randomPhoneCode);
//        redisOpsUtil.set("randomPhoneCode",randomPhoneCode);
        int returnCode = MsgUtil.sendMsg(phone,randomPhoneCode);
        if(1==returnCode){
            System.out.println("success");
            responseResult.setMessage("获取短信验证码成功");
            responseResult.setCode(1);
        }else if(2==returnCode) {
            System.out.println("fail");
            responseResult.setMessage("获取短信验证码失败");
            responseResult.setCode(2);
        }
        return responseResult;
    }

    //手机验证码的验证
    @RequestMapping("validationPhoneCode")
    @ResponseBody
    public ResponseResult validationPhoneCode(String randomPhoneCode){
        ResponseResult responseResult = new ResponseResult();
        if(Status.map.get("randomPhoneCode").equals(randomPhoneCode)){
            responseResult.setCode(1);
            responseResult.setMessage("短信验证码正确");
            Status.map.remove("randomPhoneCode");
        }else {
            responseResult.setCode(2);
            responseResult.setMessage("短信验证码错误");
        }
      return responseResult;
    }

    //点击退出之后 清除一切session
    @RequestMapping("exit")
    @ResponseBody
    public ResponseResult exit(){
        ResponseResult responseResult = new ResponseResult();
        Status.map.clear();
//        redisOpsUtil.del("phone","id","login");
        if(null != Status.map.get("login")){
            responseResult.setMessage("退出失败！");
            responseResult.setCode(2);
        }else {
            responseResult.setMessage("退出成功");
            responseResult.setCode(1);
        }
        return responseResult;
    }
}
